package com.jing.system.comm.contants;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletConfig;

import org.apache.log4j.Logger;

import com.jing.system.comm.enums.RuleType;
import com.jing.system.utils.SpringUtil;
import com.jing.weibo.pojo.system.Rule;

/**
 * 常用的类
 * @author jing.yue
 * @version 1.0
 * @since 2012-08-29
 */
public class SystemCons {

	private static Logger logger = Logger.getLogger(SystemCons.class);

	private final static String CONTANTS_PROPERTIES_URL = "/contants/contants.properties";

	/** 编码: UTF-8 */
	public final static String ENCODE_UTF_8 = "UTF-8";

	/** 第二次调用微博接口等待时间[单位: 毫秒] */
	public static final int THREAD_SLEEP_DATE = 5000;

	/** 第二次调用微博接口查询数据等待时间[单位: 毫秒] */
	public static final int THREAD_QUERY_SLEEP_DATE = 3500;

	/** 获取参与互粉的记录[默认为100] */
	public static final int JOINMP_USERINFO_NUM = 100;

	/** 获取邀请排行榜的前几位[默认为5] */
	public static final int INVITE_FRIEND_TOP_NUM = 5;

	/** 内容相似度[默认为0.6] */
	public static final double CONTENT_RESEMBLE_NUM = 0.6;

	/** 同城互粉不是VIP区域默认的ID */
	public static final Integer USER_CW_MP_VIPAREA_ID_DEFAULT = 99999999;

	/** 设置项目根目录的绝对路径 */
	public static String ABSOLUTE_PATH;

	/** 上传文件的目录 */
	public static final String UPLOAD_IMAGE_PATH = "upload" + File.separator + "temp" + File.separator;

	public static final String SUCCESS = "success";
	public static final String ERROR = "error";

	/** 分享它[发布的图片所在项目的路径] */
	public static String SYSTEM_SHARE_WEIBO_IMG_PATH = "resources" + File.separator + "images" + File.separator + "logo.png";

	/** QQ群 */
	public static String qq_group;

	/**
	 * 初始化System常用数据
	 * @param config
	 */
	public static void initSystemData(ServletConfig config) {
		Contants contants = (Contants) SpringUtil.getBean("contants");
		qq_group = contants.getQq_group();
		config.getServletContext().setAttribute("qq_group", qq_group);
		logger.info("初始化System常用数据: SUCCESS");
	}

	/** 分享微博的内容 */
	/*public final static String getUserShareWeiboContent() {
		String content = WeiboCons.user_share_weibo_content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		return content;
	}*/

	/**
	 * 参与互粉发送的微博内容
	 * @param addFansNum
	 * @return
	 */
	public static String getWeiboMpContent(Integer addFansNum) {
		String content = WeiboCons.weibo_mp_content.replaceAll("addFansNum", addFansNum.toString());
		//content = content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		return content;
	}

	/**
	 * 转发信息微博的内容
	 * @param transpondNum
	 * @param id
	 * @return
	 */
	public static String getInfoTranspondContent(Integer transpondNum, Integer id) {
		/*String content = WeiboCons.weibo_info_transpond_content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		content += "[" + transpondNum + "][" + id + "]";*/
		return WeiboCons.weibo_info_transpond_content + "~" + transpondNum + "_" + id;
	}

	/**
	 * 获取泡吧分区位置被抢的微博信息
	 * @param username
	 * @return
	 */
	public static String getCaptureContent(String username) {
		/*String content = WeiboCons.weibo_info_send_capture_content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		content = content.replaceAll("username", username);
		return content;*/
		return WeiboCons.weibo_info_send_capture_content.replaceAll("username", username);
	}

	/**
	 * 获取榜上有名的微博信息
	 * @param username		颁奖者的用户名
	 * @param receiveUser	领奖的用户昵称
	 * @return
	 */
	public static String getRankingContent(String type, String username, String receiveUser) {
		String content = null;
		//为@评论
		if(RankingCons.ATCOMMENT.equals(type)) {
			content = WeiboCons.weibo_info_ranking_atcomment;
		}
		//为评论
		else if(RankingCons.COMMENT.equals(type)) {
			content = WeiboCons.weibo_info_ranking_comment;
		}
		//为资深
		else if(RankingCons.SENIOR.equals(type)) {
			content = WeiboCons.weibo_info_ranking_senior;
		}
		return String.format(content, receiveUser, username);
	}

	/**
	 * 获取泡吧分区位置占领的邀请微博信息
	 * @return
	 */
	/*public static String getInviteCaptureContent() {
		String content = WeiboCons.weibo_info_invite_capture_content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		return content;
	}*/

	/**
	 * 获取用户每日签到发送的微博的信息
	 * @param ruleType
	 * @return
	 */
	public static String getUserSignContent(RuleType ruleType) {
		//根据code获取规则对象
		Rule rule = DataCons.ruleMap.get(ruleType.getCode());
		String content = WeiboCons.weibo_info_sign_content.replaceAll("currency", rule.getCurrency());
		content = content.replaceAll("point", rule.getPoint());
		return content;
	}

	private static Properties properties;
	static {
		//属性集合对象
		properties = new Properties();
		//将属性文件流装载到Properties对象中
		try {
			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONTANTS_PROPERTIES_URL));
		} catch (FileNotFoundException e) {
			logger.error("读取属性文件: 失败\t原因: 文件路径错误或者文件不存在");
		} catch (IOException e) {
			logger.error("装载文件: 失败\t原因: " + e.getMessage());
		}
	}

	/**
	 * 获取公共的属性文件的内容
	 * @param name			key的名字
	 * @param defaultValue	默认的值
	 * @return
	 */
	public static String getContantValue(String name, String defaultValue) {
		try {
			String value = properties.getProperty(name, defaultValue);
			logger.info("读取属性文件和值: " + name + " = " + value);
			return value;
		} catch (Exception e) {
			logger.error("未知异常\t原因: " + e.getMessage());
			e.printStackTrace();
		}
		return defaultValue;
	}
}